## Title    : CopyAsC
## License  : MIT
## Author   : WingSummer
## Function : Copy the selected bytes into C/C++ definition format.
## Written on 2022/8/2

#coding=utf-8

def main():
    if reader.currentDoc() < 0:
        service.toast("未打开文档!")
        return
    
    len = reader.selectLength()
    if len==0:
        service.toast("没有选中任何字节!")
    else:
        ba=reader.read(reader.selectionPos().offset(),len)
        buffer=bytearray(ba.data())
        s="char buffer[]={ "
        bs=[]
        for b in buffer:
            bs.append("0x%02X"%(b)) 
        s+=(','.join(bs))
        s+=" };"
        service.copy2Clipboard(s)
        service.toast("已复制到粘贴板！")

## ============= entry ============= ##

main()